import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

void main() {
  group('Date', () {
    test('不存在的日期', () {
      expect(() => Date(2023, 2, 29), isBug);
      expect(() => Date(2023, 4, 31), isBug);
      expect(() => Date(2023, 13, 1), isBug);
      expect(() => Date(2023, 0, 1), isBug);
      expect(() => Date(2023, 1, 1), returnsNormally);
      expect(() => Date(2024, 2, 29), returnsNormally);
    });

    test('今天的日期', () {
      final date = Date.now();
      final now = DateTime.now();
      expect(date.year, now.year);
      expect(date.month, now.month);
      expect(date.day, now.day);
    });

    test('解析日期', () {
      // 错误的格式
      expect(() => Date.parse('2023'), isBug);
      expect(() => Date.parse('2023-01'), isBug);
      expect(() => Date.parse('2023-01-01-01'), isBug);
      // 正确格式
      expect(() => Date.parse('2023-01-01'), returnsNormally);
      expect(() => Date.parse('2024-02-29'), returnsNormally);
    });

    test("tryParse", () {
      // 正常解析
      final date = Date.tryParse("2000-01-01");
      expect(date, isNotNull);
      expect(date!.year, 2000);
      expect(date.month, 1);
      expect(date.day, 1);
      // 无法解析
      expect(Date.tryParse(""), isNull);
      expect(Date.tryParse(null), isNull);
    });

    test('相等性', () {
      // 只要日期相同，就算是两次构造的日期也是相同的
      final date1 = Date(2023, 1, 1);
      final date2 = Date(2023, 1, 1);
      final date3 = Date(2023, 1, 2);
      expect(date1 == date2, true);
      expect(date1 == date3, false);
    });

    test('大小比较', () {
      final date1 = Date(2023, 1, 1);
      final date2 = Date(2023, 1, 2);
      final date3 = Date(2023, 2, 1);
      final date4 = Date(2024, 1, 1);
      expect(date1 < date2, true);
      expect(date1 < date1, false);
      expect(date2 < date1, false);
      expect(date1 <= date1, true);
      expect(date1 <= date2, true);
      expect(date2 <= date1, false);
      expect(date2 > date1, true);
      expect(date1 > date1, false);
      expect(date1 > date2, false);
      expect(date1 >= date1, true);
      expect(date2 >= date1, true);
      expect(date1 >= date2, false);
      expect(date1 < date3, true);
      expect(date1 < date4, true);
    });

    test('相加', () {
      final date1 = Date(2023, 1, 1);
      expect((date1 + 1).toString(), '2023-01-02');
      expect((date1 + 31).toString(), '2023-02-01');
      expect((date1 + 365).toString(), '2024-01-01');
      expect((date1 + (-1)).toString(), '2022-12-31');
    });

    test('日期平移', () {
      final date = Date(2023, 1, 1);
      expect((date - 1).toString(), '2022-12-31');
      expect((date - 31).toString(), '2022-12-01');
      expect((date - 365).toString(), '2022-01-01');
    });

    test('日期差异', () {
      final date1 = Date(2023, 1, 1);
      final date2 = Date(2023, 1, 2);
      final date3 = Date(2023, 2, 1);
      expect(date1.diff(date2), -1);
      expect(date2.diff(date1), 1);
      expect(date1.diff(date3), -31);
      expect(date3.diff(date1), 31);
    });

    test('哈希码', () {
      final date1 = Date(2023, 1, 1);
      final date2 = Date(2023, 1, 1);
      final date3 = Date(2023, 1, 2);
      expect(date1.hashCode == date2.hashCode, true);
      expect(date1.hashCode == date3.hashCode, false);
    });

    test('字符串表示', () {
      expect(Date(2023, 1, 1).toString(), '2023-01-01');
      expect(Date(2024, 12, 31).toString(), '2024-12-31');
    });

    test("计算日期所属周", () {
      expect(Date(2020, 1, 5).weekNumber, 1);
      expect(Date(2020, 1, 6).weekNumber, 2);
      expect(Date(2020, 5, 7).weekNumber, 19);
      expect(Date(2020, 12, 31).weekNumber, 53);
    });

    test("日期的比较与排序", () {
      List<Date> dates = [
        Date(2024, 1, 20),
        Date(2023, 12, 31),
        Date(2024, 2, 15),
      ];

      // 使用 compareTo 方法进行排序
      dates.sort();

      expect(dates.first, Date(2023, 12, 31));
    });
  });
}
